/**
 * Created by soul_Company on 2017/9/7 0007.
 */
/**
 * Created by alice on 2017/5/02 0001
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var nowPage = 1;
    var total;
    var ready = {
        layer: null,
        page_size: 5,
        now_page: 1
    };
    var temp = new Date();
    var defaultStartTime = temp.getFullYear() + "-01-01";
    //查询的开始时间默认给今天
    $('#startTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $('#endTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $("#endTime").find("input").val(utils.get_date(0));
    $("#startTime").find("input").val(defaultStartTime);

    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();
        Class.count_major();
        function init_event() {//TODO 初始化页面事件
            $('#major_link').attr('href', r.base_path + 'views/major/major.html?court=' + r.court_id);
            // initTable();
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
        }
    };

    function initTable(data) {
        var table = $('#table');
        table.bootstrapTable({
            columns: [{
                field: 'Number',
                title: '序号',
                width: '5%',
                class: 'cmp-table-right',
                formatter: function (value, row, index) {
                    return (index + 1) > 9 ? index + 1 : '0' + (index + 1);
                }
            }, {
                field: 'caseArchives',
                title: '案号',
                width: '10%'
            },
                //     {
                //     field: 'name',
                //     title: '名称',
                //     width: '10%',
                //     formatter: function (result,row) {
                //         var date=new Date(row.create_time);
                //         var type=row.type;
                //         var url="http://"+location.hostname+":8080/";
                //         if(type==1){
                //             //图片
                //             url=url+"images/";
                //         }else{
                //             //视频
                //             url=url+"videos/";
                //         }
                //         url=url+date.getFullYear()+"/"+(date.getMonth() + 1)+"/"+result;
                //         //拼接下载地址
                //         var a="<a  download  class='btn btn-primary' href='"+url+"'>"+result+"</a>";
                //         return a;
                //     }
                // },
                {
                    field: 'channelsName',
                    title: '通道名称',
                    width: '9%'
                }, {
                    field: 'judgeName',
                    title: '案件法官',
                    width: '9%'
                }, {
                    field: 'createUserName',
                    title: '创建人',
                    width: '9%'
                }, {
                    field: 'type',
                    title: '类型',
                    width: '9%',
                    formatter: function (result) {
                        if (result == 1) {
                            return '图片';
                        } else {
                            return '视频';
                        }
                    }
                }, {
                    field: 'dateCreated',
                    title: '创建时间',
                    width: '12%',
                    formatter: function (result) {
                        var date = new Date(result);
                        var seperator1 = "-";
                        var seperator2 = ":";
                        var month = date.getMonth() + 1;
                        if (month >= 1 && month <= 9) {
                            month = "0" + month;
                        }
                        var strDate = date.getDate();
                        if (strDate >= 1 && strDate <= 9) {
                            strDate = "0" + strDate;
                        }
                        var hours = date.getHours();
                        //时
                        if (hours >= 0 && hours <= 9) {
                            hours = "0" + hours;
                        }
                        var minute = date.getMinutes();
                        //分
                        if (minute >= 0 && minute <= 9) {
                            minute = "0" + minute;
                        }
                        //秒
                        var second = date.getSeconds();
                        //分
                        if (second >= 0 && second <= 9) {
                            second = "0" + second;
                        }
                        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                            + " " + hours + seperator2 + minute + seperator2 + second;
                        return currentdate;
                    }
                }, {
                    field: 'startTimeStr',
                    title: '截取起点',
                    width: '12%'
                }, {
                    field: 'endTimeStr',
                    title: '截取终点',
                    width: '12%'
                }, {
                    field: '',
                    title: '操作',
                    width: '10%',
                    formatter: function (result, row) {
                        var filename = row.path;
                        var url = r.base_path+'image/download?url='+'http://' + location.hostname + ':8080/cmpImages/'+row.path+'&filename='+filename;
                        var a = "<a download='"+row.caseArchives+"' href='"+url+")'><button class='btn btn-primary'>下载</button></a>";
                        var b = "<button class='btn btn-primary' onclick='del(this);' id='" + row.id + "'>删除</button>";
                        return a + b;
                    }
                }],
            data: data
        });
        $('#table').bootstrapTable('load', data);
    }

    //TODO 内部调用的方法写这里
    var Class = {
        count_major: function (pageIndex) {
            var start = $("#startTime").find("input").val();
            var end = $("#endTime").find("input").val();
            var type = $("#type").val();
            if (!pageIndex) {
                pageIndex = 1;
            }
            ready.now_page = pageIndex;
            $.post(r.base_path + "image/selectImageByCondition", {
                start: start,
                end: end,
                type: type,
                userUidList: [r.user_uid].toString(),
                page: pageIndex,
                pageSize: ready.page_size
            }, function (result) {
                var s = result.data.count;
                if (s == 0) {
                    // $("#table").html('<span class="context-white context-span" id="description">暂无截图/视频</span>');
                    Class.page(1, 1, null);
                    initTable(result.data.data);
                    $('#loading').hide();
                } else {
                    Class.page(pageIndex, Math.ceil(s / ready.page_size), Class.count_major);
                    initTable(result.data.data);
                    $('#loading').hide();
                }
            }, 'json');
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {};
    window.del = function (e) {
        var id = $(e).attr("id");
        $('#loading').show();
        $.ajax({
            "type": "post",
            "url": r.base_path + 'image/del?' + new Date().getTime(),
            "dataType": "json",
            "data": {
                id: id
            },
            "success": function (data) {
                //重新获取总页数
                Class.count_major(ready.now_page);
            },
            "error": function (data) {
                $('#loading').hide();
                alert(data.msg);
            }
        });
    }
    //检索
    window.query = function () {
        Class.count_major();
    }

}(window);

